package com.yunwaikeji.gy_tool_library.utils;

import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.yunwaikeji.gy_tool_library.info.GYViewInfo;
import com.yunwaikeji.gy_tool_library.utils.display.GYDisplayUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GYViewUtils implements GYViewInfo {
    public static List<View> getChildViewList(GetViewType getViewType, List<View> viewList) {
        List<View> resultList = new ArrayList<>();
        for (View view : viewList) {
            if (view instanceof ViewGroup) {
                ViewGroup viewGroup = (ViewGroup) view;
                for (int i = 0; i < viewGroup.getChildCount(); i++) {
                    View view1 = viewGroup.getChildAt(i);
                    switch (getViewType) {
                        case VIEW:
                            if (!(view1 instanceof ViewGroup)) {
                                resultList.add(viewGroup.getChildAt(i));
                            }
                            break;
                        case VIEW_GROUP:
                            if (view1 instanceof ViewGroup) {
                                resultList.add(viewGroup.getChildAt(i));
                            }
                            break;
                        case ALL:
                        default:
                            resultList.add(viewGroup.getChildAt(i));
                            break;
                    }
                }
            }
        }
        return resultList;
    }

    public static List<View> getChildViewList(List<View> viewList) {
        return getChildViewList(GetViewType.ALL, viewList);
    }

    public static List<View> getChildViewList(GetViewType getViewType, View... views) {
        return getChildViewList(getViewType, Arrays.asList(views));
    }

    public static List<View> getChildViewList(View... views) {
        return getChildViewList(Arrays.asList(views));
    }

    public static List<View> getAllViews(View view) {
        List<View> viewList = new ArrayList<>();
        viewList.add(view);
        if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                viewList.addAll(getAllViews(viewGroup.getChildAt(i)));
            }
        }
        return viewList;
    }

    public static SetMargin setMargin() {
        return new SetMargin();
    }

    public static class SetMargin {
        public SetMargin setMarginPx(View view, int leftPx, int topPx, int rightPx, int bottomPx) {
            ViewParent viewParent = view.getParent();
            if (viewParent != null) {
                View parentView = (View) viewParent;
                if (parentView instanceof LinearLayout) {
                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
                    layoutParams.setMargins(leftPx, topPx, rightPx, bottomPx);
                    view.setLayoutParams(layoutParams);
                } else if (parentView instanceof RelativeLayout) {
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                    layoutParams.setMargins(leftPx, topPx, rightPx, bottomPx);
                    view.setLayoutParams(layoutParams);
                } else if (parentView instanceof ConstraintLayout) {
                    ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) view.getLayoutParams();
                    layoutParams.setMargins(leftPx, topPx, rightPx, bottomPx);
                    view.setLayoutParams(layoutParams);
                }
            } else {
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
                if (layoutParams == null) {
                    layoutParams = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
                } else {
                    layoutParams.setMargins(leftPx, topPx, rightPx, bottomPx);
                }
                view.setLayoutParams(layoutParams);
            }
            return this;
        }

        public SetMargin setMarginDp(View view, int leftDp, int topDp, int rightDp, int bottomDp) {
            return setMarginPx(view, GYDisplayUtils.dp2px(view.getContext(), leftDp), GYDisplayUtils.dp2px(view.getContext(), topDp), GYDisplayUtils.dp2px(view.getContext(), rightDp), GYDisplayUtils.dp2px(view.getContext(), bottomDp));
        }
    }

    public static SetSize setSize() {
        return new SetSize();
    }

    public static class SetSize {
        public SetSize setSizeDp(View view, int widthDp, int heightDp) {
            return setSizeDp(view, widthDp, heightDp, UNCHANGED);
        }

        public SetSize setSizeDp(View view, int widthDp, int heightDp, float weight) {
            int widthPx;
            int heightPx;
            if (widthDp <= 0) {
                widthPx = widthDp;
            } else {
                widthPx = GYDisplayUtils.dp2px(view.getContext(), widthDp);
            }
            if (heightDp <= 0) {
                heightPx = heightDp;
            } else {
                heightPx = GYDisplayUtils.dp2px(view.getContext(), heightDp);
            }
            return setSizePx(view, widthPx, heightPx, weight);
        }

        public SetSize setSizePx(View view, int widthPx, int heightPx) {
            return setSizePx(view, widthPx, heightPx, UNCHANGED);
        }

        public SetSize setSizePx(View view, int widthPx, int heightPx, float weight) {
            if (view == null) {
                return this;
            }
            ViewParent viewParent = view.getParent();
            if (viewParent != null) {
                View parentView = (View) viewParent;
                if (parentView instanceof LinearLayout) {
                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
                    if (widthPx != UNCHANGED) {
                        layoutParams.width = widthPx;
                    }
                    if (heightPx != UNCHANGED) {
                        layoutParams.height = heightPx;
                    }
                    if (weight != UNCHANGED) {
                        layoutParams.weight = weight;
                    }
                    view.setLayoutParams(layoutParams);
                } else if (parentView instanceof RelativeLayout) {
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                    if (widthPx != UNCHANGED) {
                        layoutParams.width = widthPx;
                    }
                    if (heightPx != UNCHANGED) {
                        layoutParams.height = heightPx;
                    }
                    view.setLayoutParams(layoutParams);
                } else if (parentView instanceof ConstraintLayout) {
                    ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) view.getLayoutParams();
                    if (widthPx != UNCHANGED) {
                        layoutParams.width = widthPx;
                    }
                    if (heightPx != UNCHANGED) {
                        layoutParams.height = heightPx;
                    }
                    view.setLayoutParams(layoutParams);
                } else {
                    ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                    if (widthPx != UNCHANGED) {
                        layoutParams.width = widthPx;
                    }
                    if (heightPx != UNCHANGED) {
                        layoutParams.height = heightPx;
                    }
                    view.setLayoutParams(layoutParams);
                }
            } else {
                if (weight > 0) {
                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
                    if (layoutParams == null) {
                        layoutParams = new LinearLayout.LayoutParams(widthPx, heightPx, weight);
                    } else {
                        if (widthPx != UNCHANGED) {
                            layoutParams.width = widthPx;
                        }
                        if (heightPx != UNCHANGED) {
                            layoutParams.height = heightPx;
                        }
                        if (weight != UNCHANGED) {
                            layoutParams.weight = weight;
                        }
                    }
                    view.setLayoutParams(layoutParams);
                } else {
                    ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                    if (layoutParams == null) {
                        layoutParams = new LinearLayout.LayoutParams(widthPx == UNCHANGED ? WRAP_CONTENT : widthPx, heightPx == UNCHANGED ? WRAP_CONTENT : heightPx);
                    } else {
                        if (widthPx != UNCHANGED) {
                            layoutParams.width = widthPx;
                        }
                        if (heightPx != UNCHANGED) {
                            layoutParams.height = heightPx;
                        }
                    }
                    view.setLayoutParams(layoutParams);
                }
            }
            return this;
        }

        public float getWeight(View view) {
            float weight = UNKNOWN;
            ViewParent viewParent = view.getParent();
            if (viewParent != null) {
                View parentView = (View) viewParent;
                if (parentView instanceof LinearLayout) {
                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
                    weight = layoutParams.weight;
                }
            }
            return weight;
        }

        public int getWidthPx(View view) {
            int width = UNKNOWN;
            ViewParent viewParent = view.getParent();
            if (viewParent != null) {
                View parentView = (View) viewParent;
                if (parentView instanceof ViewGroup) {
                    ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                    width = layoutParams.width;
                }
            }
            return width;
        }

        public int getHeightPx(View view) {
            int height = UNKNOWN;
            ViewParent viewParent = view.getParent();
            if (viewParent != null) {
                View parentView = (View) viewParent;
                if (parentView instanceof ViewGroup) {
                    ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                    height = layoutParams.height;
                }
            }
            return height;
        }
    }

    public static AddViews addViews() {
        return new AddViews();
    }

    public static class AddViews {
        public AddViews addViews(ViewGroup viewGroup, View... views) {
            return addViews(viewGroup, Arrays.asList(views));
        }

        public AddViews addViews(ViewGroup viewGroup, List<View> viewList) {
            return addViews(viewGroup, -1, viewList);
        }

        public AddViews addViews(ViewGroup viewGroup, int index, View... views) {
            return addViews(viewGroup, index, Arrays.asList(views));
        }

        public AddViews addViews(ViewGroup viewGroup, int index, List<View> viewList) {
            for (int i = 0; i < viewList.size(); i++) {
                View view = viewList.get(i);
                if (index < 0 || index > viewGroup.getChildCount()) {
                    viewGroup.addView(view);
                } else {
                    viewGroup.addView(view, index + i);
                }
            }
            return this;
        }
    }
}
